import Taro from '@tarojs/taro';
import { computed, ref } from 'vue';

export const sysConfig = ref({});

export const HOST = ref('https://dd.7758.live');

export const IMG_HOST = ref('https://dd.7758.live');

export const APPID = ref('');

export const initExt = () => {
  APPID.value = Taro.getAccountInfoSync().miniProgram.appId;
  const extJson = Taro.getExtConfigSync() ?? {};
  console.log(APPID.value);
  console.log(extJson);
  if (extJson.appId) {
    APPID.value = extJson.appId;
    HOST.value = extJson.apiHost;
    IMG_HOST.value = extJson.imageHost;
  }
  console.log(APPID.value);
};
export const IMG_PREFIX = computed(() => {
  return IMG_HOST.value + '/api/file/preview/';
});

export const PAGE_ERR_MSG = ref('');

export const primaryColor = ref('#0079fe');

const linkCode = {
  goods: '/pages/goods/info?id={goodsId}',
  article: '/pagesBus/article/info?id={id}',
  webview: '/pages/webview/index?url={url}'
};

export const builderUri = (code, params) => {
  let url = linkCode[code];
  if (!url) {
    return url;
  }
  if (params) {
    Object.keys(params).forEach(key => {
      url = url.replace(`{${key}}`, params[key] ? encodeURIComponent(params[key]) : '');
    });
  }
  return url;
};
